﻿using System;

namespace Xwltz.Net.Tools.Browser
{
    public class InternetExplorer : BaseBrowser
    {
        public InternetExplorer(string agent)
        {
            var agent1 = agent.ToLower();

            const string ie10 = "msie";
            const string rv = "rv:";
            if (agent1.Contains(ie10))
            {
                var first = agent1.IndexOf(ie10, StringComparison.Ordinal);
                var cut = agent1.Substring(first + ie10.Length + 1);
                var version = cut.Substring(0, cut.IndexOf(';'));
                Version = ToVersion(version);
                Type = BrowserType.InternetExplorer;
            }

            if (agent1.Contains("ie 11.0"))
            {
                Type = BrowserType.InternetExplorer;
                Version = new System.Version("11.0");
            }

            if (!agent1.Contains(rv) || !agent1.Contains("trident")) return;
            {
                var first = agent1.IndexOf(rv, StringComparison.Ordinal);
                var last = agent1.IndexOf(")", first, StringComparison.Ordinal);
                if (first <= 0 || last <= 0) return;
                Type = BrowserType.InternetExplorer;
                var version = agent1.Substring(first + rv.Length, last - first - rv.Length);
                Version = new System.Version(version);
            }
        }
    }
}
